package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IActInConfirm;
import com.wbao.dianniu.listener.IActInConfirmListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;

/**
 * 1.1.1.1	报名同意/拒绝
 *
 * @author yejianjun
 */
public class ActInConfirmManger implements IActInConfirm {

    private IActInConfirmListener mListener;
    private Context mContext;

    public ActInConfirmManger(Context context) {
        mContext = context;
    }

    @Override
    public void inconfirm(int accountId, int activityId, int status) {
        // TODO Auto-generated method stub
        StringEntity stringEntity = null;
        try {
            Batch batch = new Batch();
            batch.setToken(GlobalContext.getCacheToken());
            Command command = new Command();
            command.addAction(Action.ACTION_ACCOUNT_ACTIVITY_IN_CONFIRM)
                    .addArgs("accountId", accountId)
                    .addArgs("activityId", activityId)
                    .addArgs("status", status);
            batch.addCommand(command);
            stringEntity = new StringEntity(batch.toJson(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpUtil http = new HttpUtil();
        http.req(mContext, stringEntity, new Response(accountId, activityId, status));
    }

    @Override
    public boolean addActInConfirmListener(IActInConfirmListener listener) {
        mListener = listener;
        return mListener != null;
    }

    @Override
    public boolean removeActInConfirmListener(IActInConfirmListener listener) {
        if (null == listener)
            return false;
        mListener = null;
        return true;
    }


    class Response implements MyResponse {

        private int status;
        private int activityId;
        private int accountId;

        public Response(int accountId, int activityId, int status) {
            this.status = status;
            this.activityId = activityId;
            this.accountId = accountId;
        }

        @Override
        public void responseSucess(Object obj) {
            if (null != mListener)
                mListener.onActInConfirmSuccess(accountId, activityId, status);
        }

        @Override
        public void responseFailure(int errorCode, String msg) {
            if (null != mListener)
                mListener.onActInConfirmFailure(errorCode, msg);
        }
    }

}
